import * as vscode from 'vscode';
class GeneratorText {
	private _text: string = '';
	private _prop: string = '';
	private _type: string = '';
	private _get: string = '';
	private _set: string = '';

	constructor(text: string) {
		this._text = text;
		if (text.includes(':')) {
			const s = text.replaceAll(';','').trim().split(' ')
			if (s.length == 2) {
				this._prop = s[0].substring(0, s[0].length - 1);
				this._type = s[1]
				this.buildGetSet()
			} else if (s.length == 3) {
				this._prop = s[1].substring(0, s[1].length - 1);
				this._type = s[2]
				this.buildGetSet()
			} else {
				this._text = ''
			}

		}
	}

	private buildGetSet() {
		this._get = 'public get' + this._prop.substring(0, 1).toUpperCase() + this._prop.substring(1, this._prop.length) +
			'():' + this._type + ' {\n\treturn this.' + this._prop + ';\n}'

		this.set = 'public set' + this._prop.substring(0, 1).toUpperCase() + this._prop.substring(1, this._prop.length) +
			'(value:' + this._type + ') {\n\t this.' + this._prop + ' = value;\n}'

	}

	public get text(): string {
		return this._text;
	}
	public set text(value: string) {
		this._text = value;
	}

	public get get(): string {
		return this._get;
	}
	public set get(value: string) {
		this._get = value;
	}

	public get set(): string {
		return this._set;
	}
	public set set(value: string) {
		this._set = value;
	}

}

export function activate(context: vscode.ExtensionContext) {
	let disposable = vscode.commands.registerCommand('extension.executeSelectedLine', () => {
		executeSelectedLine();
	});

	context.subscriptions.push(disposable);
}

function executeSelectedLine() {
	const editor = vscode.window.activeTextEditor;
	if (editor) {
		const selection = editor.selection;
		const params: GeneratorText[] = []
		let res = '';
		for (let index = selection.start.line; index <= selection.end.line; index++) {
			const text = new GeneratorText(editor.document.lineAt(index).text);
			if (text.text) {
				res += text.get + '\n' + text.set + '\n';
			}
		}


		editor.edit(editBuilder => {
			// 将执行结果追加到选中行的下方
			editBuilder.insert(selection.end, '\n' + res);
		});

		// 在这里执行选中行的代码
		// 这里只是一个示例，你需要根据你的需求执行实际的代码
		vscode.window.showInformationMessage(`Executing: success`);
	}
}

export function deactivate() { }
